package edu.pku.sei.modeler.uml.commands.statemachine;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Pseudostate;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.State;
import edu.pku.sei.modeler.uml.commands.NestedElementDeleteCommand;

public class AdsorbPseudostateDeleteCommand extends NestedElementDeleteCommand {
	public AdsorbPseudostateDeleteCommand(MetaNode node, MetaNode fatherNode,
			boolean isDeletedFromDiagram) {
		super(node, fatherNode, isDeletedFromDiagram);
	}
	
	@Override
	public void deleteGraphNode() {
		if (fatherNode.getParent() == null) {
			fatherNode.getDiagram().getNodes().remove(this.node);
		} else {
			fatherNode.getParent().getSubNodes().remove(this.node);
			this.node.setParent(null);
		}
	}
	
	@Override
	public void undoDeleteGraphNode() {
		if (fatherNode.getParent() == null) {
			fatherNode.getDiagram().getNodes().add(this.node);
		} else {
			this.node.setParent(fatherNode.getParent());
			fatherNode.getParent().getSubNodes().add(this.node);

		}
	}
	
	@Override
	public void deleteModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		if (fatherNode.getModelElement() instanceof State) {
			State state = (State) fatherNode.getModelElement();
			if (modelElement instanceof Pseudostate) {
				Pseudostate p = (Pseudostate) modelElement;
				if (p.getKind().getValue() == 7) {
					state.getEntry().remove(p);
				} else if (p.getKind().getValue() == 8) {
					state.getExit().remove(p);
				}
			}
		}
	}
	
	@Override
	public void undoDeleteModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		if (fatherNode.getModelElement() instanceof State) {
			State state = (State) fatherNode.getModelElement();
			if (modelElement instanceof Pseudostate) {
				Pseudostate p = (Pseudostate) modelElement;
				if (p.getKind().getValue() == 7) {
					state.getEntry().add(p);
				} else if (p.getKind().getValue() == 8) {
					state.getExit().add(p);
				}
			}
		}
	}

}
